﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.DataVisualization.Charting;

namespace WpfToolkitChart {
    using System;
    using System.Linq;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for MainWindow.xaml http://www.codeproject.com/Articles/196502/WPF-Toolkit-Charting-Controls-Line-Bar-Area-Pie-Co
    /// </summary>
    public partial class MainWindow :Window {
        List<KeyValuePair<decimal, decimal>> valueList;
        private int index;
        public MainWindow () {
            InitializeComponent ();
            showColumnChart ();
        }

        private void showColumnChart () {

            valueList = new List<KeyValuePair<decimal, decimal>> ();
            //valueList.Add(new KeyValuePair<int, int>(10, 60));
            //valueList.Add(new KeyValuePair<int, int>(15,20));
            //valueList.Add(new KeyValuePair<int, int>(18,30));
            //valueList.Add(new KeyValuePair<int, int>(22,40));
            //valueList.Add(new KeyValuePair<int, int>(13,50));
            //valueList.Add(new KeyValuePair<int, int>(11,60));


            //Setting data for column chart
            //  columnChart.DataContext = valueList;

            // Setting data for pie chart
            //  pieChart.DataContext = valueList;

            //Setting data for area chart
            // areaChart.DataContext = valueList;

            //Setting data for bar chart
            //  barChart.DataContext = valueList;

            //Setting data for line chart
            lineChart.DataContext = new ChartViewModel (lineChart);
        }

        private void CreateNewEntry (object sender, RoutedEventArgs e) {
            index++;
            var textBox = new TextBox { Height = 30, Width = 50, Margin = new Thickness (10, 0, 0, 10), BorderBrush = new SolidColorBrush (Colors.Black) };
            var textBox2 = new TextBox { Height = 30, Width = 50, Margin = new Thickness (0, 0, 0, 10), BorderBrush = new SolidColorBrush (Colors.Black) };

            var label = new Label { Content = index, Margin = new Thickness (0, 0, 0, 10) };
            var docPanel = new DockPanel ();
            docPanel.Children.Add (label);
            docPanel.Children.Add (textBox);
            docPanel.Children.Add (textBox2);
            panel.Children.Add (docPanel);

        }


        private void DrawGraph (object sender, RoutedEventArgs e) {
            var docpanels = panel.Children.OfType<DockPanel> ();
            var newList = docpanels.Select (x => new KeyValuePair<decimal, decimal> (
                     Convert.ToDecimal (x.Children.OfType<TextBox> ().First ().Text),
                 Convert.ToDecimal (x.Children.OfType<TextBox> ().Last ().Text))).ToList ();
            ( lineChart.DataContext as ChartViewModel ).Entries.Clear ();
            newList.ForEach (( lineChart.DataContext as ChartViewModel ).Entries.Add);


        }
        public class ChartViewModel :INotifyPropertyChanged {
            private readonly Chart chart;
            public ObservableCollection<KeyValuePair<decimal, decimal>> Entries { get; set; }

            public ChartViewModel (Chart chart)
            {
                this.chart = chart;
                Entries = new ObservableCollection<KeyValuePair<decimal, decimal>> ();
            }

            private double minX1;
            public double MinX {
                get { return minX1; }
                set {
                    minX1 = value;
                    RaisePropertyChanged ("MinX");
                }
            }

            private double minY1;
            public double MinY {
                get { return minY1; }
                set {
                    minY1 = value;
                    
                    RaisePropertyChanged ("MinY");
                }
            }

            private double maxY1;
            public double MaxY {
                get { return maxY1; }
                set {
                    maxY1 = value;
                    RaisePropertyChanged ("MaxY");
                }
            }

            private double maxX1;
            public double MaxX {
                get { return maxX1; }
                set {
                    maxX1 = value;
                    RaisePropertyChanged ("MaxX");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void RaisePropertyChanged (string propertyName) {
                // take a copy to prevent thread issues
                PropertyChangedEventHandler handler = PropertyChanged;
                if ( handler != null ) {
                    handler (this, new PropertyChangedEventArgs (propertyName));
                }
            }
        }
    }
}
